package com.icoolkj.mall.account.mapper;

import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.math.BigDecimal;
import java.util.Date;

/**
 * @author icoolkj
 * @version 1.0
 * @description
 * @createDate 2025/01/16 17:06
 */
@Mapper
public interface AccountMapper {

    @Select("SELECT account_balance FROM account WHERE user_id = #{userId}")
    BigDecimal getBalance(@Param("userId") Long userId);

    @Update("UPDATE account SET account_balance = account_balance - #{orderCost}, update_time = #{updateTime} WHERE user_id = #{userId} AND account_balance >= ${orderCost}")
    int reduceBalance(@Param("userId") Long userId, @Param("orderCost") BigDecimal orderCost,
                      @Param("updateTime") Date updateTime);

}
